package org.otherworld.frame.textwriter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public final class TextWriter extends JFrame{

	private static final long serialVersionUID = 1L;
	private JMenuBar menubar;
	private JMenu menu;
	private JTextArea editor;
	//private JPanel controlPanel;
	private JPanel infoPanel;
	private String mode;
	public static JLabel characterInfo; 
	public static long characters;
	private final String EDIT = "edit";
	private final String VIEW = "view";
	
	/**
	 * Konstruktor
	 */
	public TextWriter() {
		int windowWidth = 800;
		int windowHeight = 600;
		this.setSize(windowWidth, windowHeight);
		this.setLayout(new BorderLayout());
		
		if (this.mode == EDIT) {
			this.initEditorControl();
			this.initEditorField();
			this.initEditorInfoBorder();
			this.initMenu();
		} else if (this.mode == VIEW) {
			this.initEditorField();
			this.initEditorInfoBorder();
		}
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public void setMode(String mode) {
		this.mode = mode;
	}
	
	private StringBuilder getFileContents(File file) throws Exception{
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = "";
		StringBuilder text = new StringBuilder();
		while ((line = reader.readLine())!=null) {
			text.append(line);
			text.append(System.getProperty("line.separator"));
		}	
		reader.close();
		return text;
	}
	
	public TextWriter(File file,String mode) throws Exception{
		this.mode = mode;
		int windowWidth = 800;
		int windowHeight = 600;
		this.setSize(windowWidth, windowHeight);
		this.setLayout(new BorderLayout());
		
		if (this.mode == EDIT) {
			this.initEditorControl();
			this.initEditorField();
			this.initEditorInfoBorder();
			this.initMenu();
		} else if (this.mode == VIEW) {
			this.initEditorField();
			this.initEditorInfoBorder();
		}
		this.setVisible(true);
		/** Módosítani az integráció miatt **/
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	
		StringBuilder fileContent = this.getFileContents(file);
		editor.setText(fileContent.toString());
		
	}
	/** 
	 * Kihelyzi a szerkesztő ablakot
	 */
	private void initEditorField() {
		this.editor = new JTextArea(100,100);
		this.editor.setMinimumSize(new Dimension(800,400));
		this.add(editor,BorderLayout.CENTER);
		if (this.mode == VIEW) {
			this.editor.setEditable(false);
		}
		
		this.editor.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				characterInfo.setText("Character count: "+editor.getText().length());
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		
	}
	
	/**
	 * Ki helyezi a kontrolltáblát
	 */
	private void initEditorControl() {
		//controlPanel = new JPanel();
		
	}

	/**
	 * Kihelyezi az információs sávot (alulra)
	 */
	private void initEditorInfoBorder() {
		infoPanel = new JPanel();
		String text = "Character count: ";
		text = text += editor.getText().length();
		characterInfo = new JLabel(text);
		infoPanel.add(characterInfo);
		this.add(infoPanel,BorderLayout.SOUTH);
	}
	
	/**
	 * Kihelyzi a menüt
	 */
	private void initMenu() {
		menubar = new JMenuBar();
		menu = new JMenu("File");
		JMenuItem newDoc = new JMenuItem("New");
		newDoc.addActionListener(new ActionNewDocument(editor));
		JMenuItem load = new JMenuItem("Load");
		load.addActionListener(new ActionLoadDocument(editor));
		JMenuItem save = new JMenuItem("Save");
		save.addActionListener(new ActionSaveDocument(editor));
		menu.add(newDoc);
		menu.add(load);
		menu.add(save);
		menubar.add(menu);
		menubar.setVisible(true);
		add(menubar,BorderLayout.NORTH);	
	}
}
